"""
Case Type   : cm
Case Name   : cm_ctl set设置cm参数基本参数验证
Create At   : 2024-07
Owner       : haomeng
Description :
    1、查看集群状态
    2、修改agent参数并动态加载
    3、查看server和agent被修改参数值
    4、设置使用错误参数
    5、设置不存在的参数
    6、恢复初始环境
Expect      :
    1、集群所有节点状态正常，显示Normal
    2、修改成功，加载成功
    3、server参数未更新，agent参数更新
    4、合理报错
    5、合理报错
    6、恢复初始环境成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class CmCtl(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.comsh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.constant = Constant()
        self.server_param = 'thread_count'
        self.agent_param = 'security_mode'

        self.log.info('---获取待修改参数原始默认值---')
        res1 = self.pri_user.sh(f'source {macro.DB_ENV_PATH};'
                                f'cm_ctl list --param --server | grep {self.server_param}').result()
        self.server_default = res1.splitlines()[0].split('=')[1]
        self.log.info(self.server_default)

        res2 = self.pri_user.sh(f'source {macro.DB_ENV_PATH};'
                                f'cm_ctl list --param --agent | grep {self.agent_param}').result()
        self.agent_default = res2.splitlines()[0].split('=')[1]
        self.log.info(self.agent_default)

    def test_cm(self):
        text = '----step1:查看集群状态; expect:集群状态正常,DN主在节点1----'
        self.log.info(text)

        status = self.comsh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn("cluster_state   : Normal", status)

        text = '----step2:修改agent参数并动态加载; expect:修改成功，加载成功----'
        self.log.info(text)

        set_res1 = self.comsh.exec_cm_ctl('set',f'--param --server -k {self.server_param}=66 '
                                                f'--agent -k {self.agent_param}="on"')
        self.log.info(set_res1)
        self.assertIn(self.constant.cma_set_success_msg, set_res1, '设置参数失败')

        reload_res1 = self.comsh.exec_cm_ctl('reload', '--param --agent')
        self.log.info(reload_res1)
        self.assertIn(self.constant.cma_reload_success_msg, reload_res1, '加载失败')

        text = '----step3:查看server和agent被修改参数值  expect:server参数未更新，agent参数更新----'
        self.log.info(text)

        cat_res1 = self.pri_user.sh(f'source {macro.DB_ENV_PATH};'
                                    f'cm_ctl list --param --server | grep {self.server_param}').result()
        self.server_value = cat_res1.splitlines()[0].split('=')[1]
        self.log.info(self.server_default)
        self.assertEqual(self.server_default, self.server_value, 'server参数不一致')

        cat_res2 = self.pri_user.sh(f'source {macro.DB_ENV_PATH};'
                                    f'cm_ctl list --param --agent | grep {self.agent_param}').result()
        self.agent_value = cat_res2.splitlines()[0].split('=')[1]
        self.log.info(self.agent_value)
        self.assertNotEqual(self.agent_default, self.agent_value, 'agent参数一致')

        text = '----step4:指定错误参数  expect:合理报错----'
        self.log.info(text)

        param_list1 = ['--param', '--server', '--agent', '--param --server']
        for param in param_list1:
            set_res2 = self.comsh.exec_cm_ctl('set', f'{param}')
            self.assertIn('Please check the cmdline', set_res2, '参数设置成功')

        text = '----step5:指定不存在的参数  expect:合理报错----'
        self.log.info(text)

        set_res3 = self.comsh.exec_cm_ctl('set', f'--param --agent -k {self.server_param}=66')
        self.log.info(set_res3)
        self.assertIn('incorrect', set_res3, '设置成功')

        set_res4 = self.comsh.exec_cm_ctl('set', f'--param --server -k {self.agent_param}="on"')
        self.log.info(set_res4)
        self.assertIn('incorrect', set_res4, '设置成功')

    def tearDown(self):
        text = '----step5:恢复初始集群状态  expect:恢复初始集群状态成功----'
        self.log.info(text)

        reset1 = self.comsh.exec_cm_ctl('set', f'--param --server '
                                               f'-k {self.server_param}={self.server_default}')
        reload1 = self.comsh.exec_cm_ctl('reload','--param --server')
        self.log.info(reset1)
        self.log.info(reload1)

        reset2 = self.comsh.exec_cm_ctl('set', f'--param --agent '
                                               f'-k {self.agent_param}={self.agent_default}')
        reload2 = self.comsh.exec_cm_ctl('reload','--param --agent')
        self.log.info(reset2)
        self.log.info(reload2)

        self.assertIn(self.constant.cms_reload_success_msg, reload1, '恢复server参数成功')
        self.assertIn(self.constant.cma_reload_success_msg, reload2, '恢复agent参数成功')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
